package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
	. "zhhuiyan/models"
	. "zhhuiyan/authority"
	. "zhhuiyan/db"
)

type ModelController struct {
	beego.Controller
}

func (this *ModelController) Set(tpl string) {
	this.Data["Path"] = "model"
	this.Layout = "layout.html"
	this.TplNames = "model/"+tpl
}
func (this *ModelController) Get() {
	id, _ := this.GetInt(":objectId")
	var model = &Model{Id:id, Object:&Object{Id:id}}
	if id != 0 {
		if err := Handler.Read(model); err != nil {
			fmt.Println(err)
		}
		if (model.Object != nil) {
			if err := Handler.Read(model.Object); err != nil {
				fmt.Println(err)
			}
		}
		this.Data["Model"] = model
		this.Set("show.html")
		return
	}
	this.Data["Model"] = model
	this.Set("add.html")
}
func (this *ModelController) Post() {
	object := &Object{Name: this.GetString("name")}
	if _, err := Handler.Insert(object) ; err != nil {
		fmt.Println(err)
	}
	model := &Model{ Object:object}
	if _, err := Handler.Insert(model) ; err != nil {
		fmt.Println(err)
	}
	this.Data["Model"] = model
	this.Set("show.html")
}


